define([
	'../libs/zepto',
	'../common/common_click',
	'../common/dialog',
	'../common/yingSoftPrg',
	'../common/yingSoftCookie',
	'../common/ajax'
],function(){
	this.init = function(){
		var type = 1;
		var data = _getRankingData(type);
		$('#loadingDiv').css('display','none');
		setCss();
		rankdata(data);
		_click();
	}
	 
	function setCss(){
		var topStr = '<h3 class="topRank"><img src="/images/gaoshou.png"/>'+
					 '<span>高手榜</span></h3><div class="detail"><span>正确答题最多的英豪都在这里了</span></div>';
					// '<b class="time">2016-11-30</b></div>';
		$('.explain').html(topStr);
//		var img_width = $('.topRank img').width() + 24;
//		console.log(img_width);
		$('.topRank span').css('padding-left',24+'px');
		
		var whole_height = window.innerHeight;
		var head_height = $('.common_header').height();
		var top_height = $('.explain').height();
		var content_height = whole_height - head_height - top_height;
		$('.index-containt').css('height',content_height);
	}
	
	
	function rankdata(getData){
		if(getData){
			for(var i = 0; i < getData.length; i ++){
				var str = '';
				switch(i){
					case 0:
						str = '<li class="li_list" username="'+ getData[i].NickName +'" userid="'+ getData[i].UserID +'"><i class="pai"><img src="/images/jinpai.png" /></i>'+
							'<img src="/images/touxiang.png" class="touxiang"/><div class="info">'+
							'<p class="nicheng">'+ getData[i].NickName +'</p><span class="count bg">'+ getData[i].Value +'</span></div>'+
							'<span class="lianjie"><img src="/images/btn@.png"/></span></li>';
					break;
					case 1:
						str = '<li class="li_list" username="'+ getData[i].NickName +'" userid="'+ getData[i].UserID +'"><i class="pai"><img src="/images/yinpai.png" /></i>'+
							'<img src="/images/touxiang.png" class="touxiang"/><div class="info">'+
							'<p class="nicheng">'+ getData[i].NickName +'</p><span class="count bg1">'+ getData[i].Value +'</span></div>'+
							'<span class="lianjie"><img src="/images/btn@.png"/></span></li>';
					break;
					case 2:
						str = '<li class="li_list" username="'+ getData[i].NickName +'" userid="'+ getData[i].UserID +'"><i class="pai"><img src="/images/tongpai.png" /></i>'+
							'<img src="/images/touxiang.png" class="touxiang"/><div class="info">'+
							'<p class="nicheng">'+ getData[i].NickName +'</p><span class="count bg2">'+ getData[i].Value +'</span></div>'+
							'<span class="lianjie"><img src="/images/btn@.png"/></span></li>';
					break;
					default:
						str = '<li class="li_list" username="'+ getData[i].NickName +'" userid="'+ getData[i].UserID +'"><i class="pai number">'+ (i+1) +'</i><img src="/images/touxiang.png" class="touxiang"/>'+
							  '<div class="info"><p class="nicheng">'+ getData[i].NickName +'</p><span class="count">'+ getData[i].Value +'</span></div>'+
							  '<span class="lianjie"><img src="/images/btn@.png"/></span></li>'	;
				}
				$('.ul_list').append(str);
				myscroll.refresh();
			}
			_isOnlineFQA();
		}else{
			G_Prg.alert('暂时无排行信息',function(){
				window.location.href = '/doExam-index';
			});
		}
	
	}
	
	function _click(){
		$('.back').click(function(){
			window.location.href = '/doExam-index';
		});
		
		$('.lianjie').click(function(){
			console.log($(this).parent().index());
			var userID = $(this).parent().attr('userid');
			var userName = $(this).parent().attr('username');
			window.location.href = '/onlineFAQ?fromUrl=rankList&&userID='+ userID +'&&username='+ userName;
		});
		
		$('.help').click(function(){
			window.location.href = '/explain';
		});
		
	}
	 
	init();
	
	// 获取排行信息
	function _getRankingData(type) {
		var appID = G_Cookie.getAppID();
		if(!appID) {
			G_Prg.throw('程序运行错误，rankingListUI._getRankingData : appID = "' + appID + '",获取数据出现异常');
		}
		var _rankingJsonData = [];
		var _success = function(json) {
			var jsonData = JSON.parse(json);
			if(jsonData.status === 200) {
				_rankingJsonData = jsonData.data;
			} else if(jsonData.status === 201) {
				G_Prg.alert('暂时无排行榜数据',function(){
					window.location.href = '/doExam-index';
				});
			} else {
				if(jsonData.status === 300) {
					
				} else if(jsonData.status >= 400 && jsonData.status < 500) {
					
				} else {
					
				}
				G_Prg.throw('程序运行错误，不能处理，rankingListUI._getRankingData =' + jsonData.status + ':' + jsonData.msg);
			}
		}
		G_AjaxApi_Record.get('/api/ranking/list?appID=' + appID + '&type=' + type, false, _success);
		//_showRankingList(type);
		return _rankingJsonData;
	}
});


/********************************************************************************
 函数名：_isOnlineFQA
 功能：判断是否有在线答疑
 输入参数：无
 返回值：无
 *******************************************************************************/
function _isOnlineFQA() {
	var appID = G_Cookie.getAppID();
	if (!appID) {
		G_Prg.throw('程序运行错误，defaultPage._isOnlineFQA ,不能处理appID=' + appID);
	}
	var URL = '/api/app/getAppInfo?appID=' + appID;
	var success = function (json) {
		var jsonData = JSON.parse(json);
		if (jsonData.status === 200) {
			var isHasFQA = jsonData.data.hasOnlineSolve;
			if(isHasFQA === 1){
				$('.lianjie').css('display','block');
			}else{
				$('.lianjie').css('display','none');
			}
		} else if (jsonData.status === 201) {

		} else {
		}
	};
	G_AjaxApi_Record.get(URL, true, success);
}
